﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.FMCG.Buz.Interfaces;
using Microhard.Core.DBUtility;
using Microhard.FMCG.Buz.DataAccess;
using System.Data.Common;
using Microhard.Core;
using Microhard.FMCG.Buz.Models;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;

namespace Microhard.FMCG.Buz.Impls
{
    public class NewsImpl : INews
    {
        public Core.DBUtility.ExecutedResult Add(Models.NewsModel model)
        {
            ExecutedResult result = new ExecutedResult();
            string addSql = "insert into news(Title,Detail,DetailLink,PublishDate,CreateDate) values(@Title,@Detail,@DetailLink,@PublishDate,@CreateDate)";
            List<SqlParameter> parms = new List<SqlParameter>();
            parms.Add(new SqlParameter("@Title", model.Title));
            parms.Add(new SqlParameter("@Detail", model.Detail));
            parms.Add(new SqlParameter("@DetailLink", model.DetailLink));
            parms.Add(new SqlParameter("@PublishDate", model.PublishDate));
            parms.Add(new SqlParameter("@CreateDate", DateTime.Now));

            try
            {
                var rtn = SqlHelper.ExecuteNonQuery(DBHelper.GetCNN(),
                    System.Data.CommandType.Text,
                    addSql,
                    parms.ToArray());
            }
            catch (Exception ex)
            {
                Log.Error("NewsImpl.Add", ex);
                result.ErrorMsg.Add("Add", "添加新闻失败");
            }
            return result;
        }

        public Core.DBUtility.ExecutedResult Update(Models.NewsModel model)
        {
            throw new NotImplementedException();
        }

        public Core.DBUtility.ExecutedResult Delete(List<Guid> ids)
        {
            throw new NotImplementedException();
        }

        public Core.DBUtility.ExecutedResult Delete(Models.NewsModel model)
        {
            throw new NotImplementedException();
        }

        public Models.NewsModel GetModelByID(Guid id)
        {
            throw new NotImplementedException();
        }

        public Models.NewsModel GetModelByModel(Models.NewsModel model)
        {
            NewsModel result = new NewsModel();
            string addSql = "select * from news where ID=@ID";
            List<SqlParameter> parms = new List<SqlParameter>();
            parms.Add(new SqlParameter("@ID", model.NewsID));
            try
            {
                var ds = SqlHelper.ExecuteDataset(DBHelper.GetCNN(),
                    System.Data.CommandType.Text,
                    addSql,
                    parms.ToArray());
                result = new NewsModel
                {
                    CreateDate = DateTime.Parse(ds.Tables[0].Rows[0]["CreateDate"].ToString()),
                    Detail = ds.Tables[0].Rows[0]["Detail"].ToString(),
                    DetailLink = ds.Tables[0].Rows[0]["DetailLink"].ToString(),
                    NewsID = long.Parse(ds.Tables[0].Rows[0]["NewsID"].ToString()),
                    PublishDate = DateTime.Parse(ds.Tables[0].Rows[0]["PublishDate"].ToString()),
                    Title = ds.Tables[0].Rows[0]["Title"].ToString()
                };
            }
            catch (Exception ex)
            {
                Log.Error("OrderImpl.Query", ex);
            }
            return result;
        }

        public List<Models.NewsModel> Query(Models.NewsModel Model, int pageIndex, int pageSize, out int totalCount)
        {
            throw new NotImplementedException();
        }

        public List<Models.NewsModel> Query(Models.NewsModel Model)
        {
            List<Models.NewsModel> result = new List<Models.NewsModel>();
            string addSql = "select * from news where 1=1 ";
            List<SqlParameter> parms = new List<SqlParameter>();
            if (string.IsNullOrEmpty(Model.Title))
            {
                addSql += " and Title like @Title ";
                parms.Add(new SqlParameter("@Title", Model.Title));
            }
            if (string.IsNullOrEmpty(Model.Detail))
            {
                addSql += " and Detail like @Detail ";
                parms.Add(new SqlParameter("@Detail", Model.Detail));
            }
            if (Model.QueryEnd >= Model.QueryStart)
            {
                addSql += " and PublishDate between @QueryStart and @QueryEnd ";
                parms.Add(new SqlParameter("@QueryStart", Model.QueryStart));
                parms.Add(new SqlParameter("@QueryEnd", Model.QueryEnd));
            }

            try
            {
                var ds = SqlHelper.ExecuteDataset(DBHelper.GetCNN(),
                    System.Data.CommandType.Text,
                    addSql,
                    parms.ToArray());
                for (int n = 0; n < ds.Tables[0].Rows.Count; n++)
                {
                    result.Add(new Models.NewsModel
                    {
                        CreateDate = DateTime.Parse(ds.Tables[0].Rows[n]["CreateDate"].ToString()),
                        Detail = ds.Tables[0].Rows[n]["Detail"].ToString(),
                        DetailLink = ds.Tables[0].Rows[n]["DetailLink"].ToString(),
                        NewsID = long.Parse(ds.Tables[0].Rows[n]["NewsID"].ToString()),
                        PublishDate = DateTime.Parse(ds.Tables[0].Rows[n]["PublishDate"].ToString()),
                        Title = ds.Tables[0].Rows[n]["Title"].ToString()
                    });
                }
            }
            catch (Exception ex)
            {
                Log.Error("NewsImpl.Query", ex);
            }
            return result;
        }

        public Core.DBUtility.ScopeResult scope
        {
            set;
            get;
        }
    }
}
